/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package frsf.tibus.smsclient;

import frsf.tibus.domain.PredictionRequest;
import frsf.tibus.domain.PredictionResponse;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;
import java.util.Random;

/**
 *
 * @author dani
 */
public class PredictionRequester implements MessageListener {

    private static int ackMode;
    private static String clientQueueName;

    private boolean transacted = false;
    private MessageProducer producer;

    static {
        clientQueueName = "predictions.requests";
        ackMode = Session.AUTO_ACKNOWLEDGE;
    }

    public PredictionRequester() {
        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");
        Connection connection;
        try {
            connection = connectionFactory.createConnection();
            connection.start();
            Session session = connection.createSession(transacted, ackMode);
            Destination adminQueue = session.createQueue(clientQueueName);

            //Setup a message producer to send message to the queue the server is consuming from
            this.producer = session.createProducer(adminQueue);
            this.producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);

            //Create a temporary queue that this client will listen for responses on then create a consumer
            //that consumes message from this temporary queue...for a real application a client should reuse
            //the same temp queue for each message to the server...one temp queue per client
            Destination tempDest = session.createTemporaryQueue();
            MessageConsumer responseConsumer = session.createConsumer(tempDest);

            //This class will handle the messages to the temp queue as well
            responseConsumer.setMessageListener(this);

            //Now create the actual message you want to send
            ObjectMessage objMessage = session.createObjectMessage();

            objMessage.setObject(new PredictionRequest());


            //Set the reply to field to the temp queue you created above, this is the queue the server
            //will respond to
            objMessage.setJMSReplyTo(tempDest);

            //Set a correlation ID so when you get a response you know which sent message the response is for
            //If there is never more than one outstanding message to the server then the
            //same correlation ID can be used for all the messages...if there is more than one outstanding
            //message to the server you would presumably want to associate the correlation ID with this
            //message somehow...a Map works good
            String correlationId = this.createRandomString();
            objMessage.setJMSCorrelationID(correlationId);
            this.producer.send(objMessage);
        } catch (JMSException e) {
            //Handle the exception appropriately
        }
    }

    private String createRandomString() {
        Random random = new Random(System.currentTimeMillis());
        long randomLong = random.nextLong();
        return Long.toHexString(randomLong);
    }

    public void onMessage(Message message) {
            
            try {

            if (message instanceof ObjectMessage) {
                PredictionResponse pr = (PredictionResponse) ((ObjectMessage)message).getObject();
                 System.out.println("messageText = " + pr.getPrediction());
                }
            } catch (JMSException ex) 
            {
                Logger.getLogger(PredictionRequester.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
